package app.solaris;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityLogin extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        // Buttons in template opzoeken
        final Button btnLogin = (Button) findViewById(R.id.btnLogin);
        final EditText txtUsername = (EditText) findViewById(R.id.txtUsername);
        final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
        
        // Onclick op de button gooien om inloggen mogelijk te maken
        btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                
                // We checken of er internet is
                Network network = Network.instantiate();
                boolean connectionOk = network.checkInternetConnection(ActivityLogin.this);
                
                if(connectionOk){
                
                    // Gebruikersnaam en wachtwoord ophalen uit inlogvelden
                    String strUsername = txtUsername.getText().toString();
                    String strPassword = txtPassword.getText().toString();

                    Connection connection = Connection.instantiate();
                    boolean loginOk = connection.checkLogin(strUsername, strPassword);
                    
                    if(loginOk) {
                        
                        // Sessie van gebruiker aanzetten
                        Session session = Session.instantiate();
                        session.setAuthentication(true);
                        session.setUsername(strUsername);
                        session.setPassword(strPassword);

                        // Gebruiker doorsturen naar hoofdmenu
                        Intent i = new Intent(ActivityLogin.this, ActivityObservation.class);
                        startActivity(i);
                        finish();
                        
                    } else {
                        
                        // Inlogvelden resetten
                        txtUsername.setText("");
                        txtPassword.setText("");
                        
                        // Melding weergeven aan gebruiker
                        AlertDialog alertDialog = new AlertDialog.Builder(ActivityLogin.this).create();               
                        alertDialog.setTitle("Login mislukt");
                        alertDialog.setMessage("Controleer uw gebruikersnaam en wachtwoord en probeer opnieuw");
                        alertDialog.setButton("OK", new OnClickListener(){
                            public void onClick(DialogInterface dialog, int which){
                                // Dummy functie
                            }
                        });
                        alertDialog.show();
                        
                    }
                    
                }
                
            }
            
        });
        
    }
}
